
<template>
  <div>
    <el-card>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>课程负责人</el-breadcrumb-item>
        <el-breadcrumb-item>课程信息管理</el-breadcrumb-item>
        <el-breadcrumb-item>课程先修后修</el-breadcrumb-item>
      </el-breadcrumb>
      <div class="searchForm">

        <el-form :inline="true" class="demo-form-inline">
          <el-form-item>
            <el-input v-model="queryInfo.query" placeholder="请输入课程名称" clearable @clear="getCourseList"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="" @click="getCourseList" icon="el-icon-search">查询</el-button>
          </el-form-item>
          <el-button style="position: absolute; right: 720px" type="" @click="addDialogVisible=true">添加关系</el-button>
          <el-button style="position: absolute; right: 100px" @click="exports">导出PDF</el-button>
        </el-form>

        <el-dialog title="添加关联信息" :visible.sync="addDialogVisible" width="40%" @close="addDialogClose">
          <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="150px">

            <el-form-item label="对应修习关系" prop="coursetype" >
              <el-select v-model="addForm.coursetype" >
                <el-option label="先修" value="先修"></el-option>
                <el-option label="后修" value="后修"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="对应课程名称" prop="relacname">
              <el-select v-model="addForm.relacname" placeholder="请选择" @change="getCname()" clearable>
                <el-option v-for="item in CnameList" :key="item.cname" :label="item.cname" :value="item.cname">
                </el-option>
              </el-select>
            </el-form-item>


          </el-form>

          <div slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCourserelation">确 认</el-button>
          </div>
        </el-dialog>





        <div id = "pdfDom">
          <el-table :header-cell-style="{background:'#808080',color:'#FFFFFF'}" border stripe :data="List" style="width: 100%">

            <el-table-column prop="coursetype" label="修习关系" width="300" align="center">
            </el-table-column>

            <el-table-column prop="relacname" label="对应课程名称" width="400" align="center">
            </el-table-column>


            <el-table-column label="删除" align="center">
              <template slot-scope="scope">
                <el-tooltip class="item" effect="dark" content="删除关联" placement="top-start">
                  <el-button type="" icon="el-icon-delete" circle @click="deleteParagraph(scope.row.courseid,scope.row.relacname)"></el-button>
                </el-tooltip>
              </template>
            </el-table-column>


          </el-table>
        </div>

      </div>

      <div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[4]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>


<script>
  export default {

    created(){
      this.getCourseList();
      this.getCname();
    },
    data() {
      return {
        fileName: this.$session.get('coursename') + '课程先修后修信息',
        queryInfo:{
          query:'',//查询信息
          role:this.$session.get('bulls'),
          pageNum:1,
          pageSize:4
        },
        queryInfo1:{
          query:'',//查询信息
          role:'',
          pageNum:1,
          pageSize:50
        },
        List:[],
        CnameList:[],
        total:0,
        addDialogVisible:false,
        editDialogVisible:false,
        infoDialogVisible:false,
        Form:{
          courseid:'',
          coursetype:'',
          relacourseid:'',
          relacname:'',

        },
        addForm:{
          courseid:this.$session.get('bulls'),
          cname:this.$session.get("coursename"),
          coursetype:'',
          relacourseid:'',
          relacname:'',

        },
        editFormRules: {

          coursetype: [
            { required: true, message: '请输入关联关系', trigger: 'blur' },

          ],

          relacname: [
            { required: true, message: '请输入对应课程名称', trigger: 'blur' },

          ]

        },
        addFormRules: {

          coursetype: [
            { required: true, message: '请输入修习关系', trigger: 'blur' },

          ],
          relacourseid: [
            { required: true, message: '请输入对应课程编号', trigger: 'blur' },
          ],

          relacname: [
            { required: true, message: '请输入对应课程名称', trigger: 'blur' },

          ],

        },
        FormRules: {

        },



      }
    },


    methods: {
      exports () {
        this.getPdf('pdfDom', this.fileName)
      },
      async getCourseList () {
        const { data: res } = await this.$http.get("CourseRelations/AllCourseRelation" ,{ params: this.queryInfo });
        this.List = res.data;
        this.total = res.numbers;
      },
      async getCname () {
        const { data: res } = await this.$http.get("Courses/AllCname" ,{ params: this.queryInfo1 });
        this.CnameList = res.data;
      },

      handleSizeChange (newSize) {
        this.queryInfo.pageSize = newSize;
        this.getCourseList();
      },
      handleCurrentChange (newPage) {
        this.queryInfo.pageNum = newPage;
        this.getCourseList();
      },
      addDialogClose () {
        this.$refs.addFormRef.resetFields();
      },
      editDialogClose () {
        this.$refs.editFormRef.resetFields();
      },
      ret(){
        this.$router.push({ path: "../manageCourse"});
      },
      async deleteParagraph (courseid,relacname) {

        const confirmResult = await this.$confirm('是否删除该关联？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if (confirmResult !== 'confirm') {
          return ;
        }
        const { data: res } = await this.$http.delete("CourseRelations/CourseRelation?courseid="+ courseid+"&relacname="+relacname+"");
        if (res !== "success") {
          return this.$message.error("删除失败");
        }
        this.$message.success("删除成功");
        this.getCourseList();
      },

      addCourserelation () {
        if(this.addForm.relacname === this.$session.get("coursename")){
          this.addDialogVisible = false;
          return this.$message.error("请不要添加当前课程为先修或者后修关系！!")
        }
        for(var i = 0;i < this.List.length;i++){
          if(this.addForm.relacname == this.List[i].relacname){
            return this.$message.error("请不要添加已有的先修/后修课程!")
          }
        }

        this.$refs.addFormRef.validate(async valid => {
          if (!valid) return;
          const { data: res } = await this.$http.post("CourseRelations/CourseRelation", this.addForm);
          if (res !== "success") {
            return this.$message.error("添加失败!")
          }
          this.$message.success("添加成功！")
          this.addDialogVisible = false;
          this.getCourseList();
        })
      },
    }
  }
</script>




<style scoped>
  .el-header{
    background: lightslategray;
    color: white;
    font-size: 25px;
    align-items: center;
    justify-content: space-between;
    display: flex;
  }
  .el-main{
    background-color: whitesmoke;
  }

</style>
